#include "stdafx.h"
#include <boost/lexical_cast.hpp>
#include "../Engine/InputManager.hpp"

namespace he
{
	namespace
	{
		//! Get param list for OIS.
		size_t params_for_window(Ogre::RenderWindow &Window)
		{
			
			size_t window_hwnd;
			//tell OIS about the Ogre window
			Window.getCustomAttribute("WINDOW", &window_hwnd);

			return window_hwnd;
		}
	}

	InputManager::InputManager(Ogre::RenderWindow &Window)
		: m_pOIS(OIS::InputManager::createInputSystem(params_for_window(Window))),
		m_pKeyboard(static_cast<OIS::Keyboard*>(m_pOIS->createInputObject(OIS::OISKeyboard, true))),
		m_pMouse(static_cast<OIS::Mouse*>(m_pOIS->createInputObject(OIS::OISMouse, true))),
		m_KeyState()
	{
		const OIS::MouseState &ms = m_pMouse->getMouseState();
		ms.width = Window.getWidth();
		ms.height = Window.getHeight();
	}

	InputManager::~InputManager()
	{
		m_pOIS->destroyInputObject(m_pMouse);
		m_pOIS->destroyInputObject(m_pKeyboard);
		OIS::InputManager::destroyInputSystem(m_pOIS);
	}

	void InputManager::update()
	{
		// Need to save old state
		std::copy(m_KeyState, m_KeyState + 256, m_KeyStateOld);
		// Update the input devices
		m_pKeyboard->capture();
		m_pMouse->capture();
		// Get state of all keys
		m_pKeyboard->copyKeyStates(m_KeyState);
	}

	bool InputManager::is_key_up(OIS::KeyCode Key) const
	{
		return (m_KeyStateOld[Key] == 1 && m_KeyState[Key] == 0);
	}

	bool InputManager::is_key_down(OIS::KeyCode Key) const
	{
		return (m_KeyStateOld[Key] == 1);
	}

} /*he*/